package com.yy.young.travel.web;

import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.Result;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.util.Page;
import com.yy.young.interfaces.log.annotation.Log;
import com.yy.young.travel.model.Item;
import com.yy.young.travel.service.IItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 项目服务
 * Created by hua on 2018-03-22.
 */
@Controller
@RequestMapping("/travel/item")
public class ItemController {

    @Resource(name="itemService")
    IItemService service;

    /**
     * 获取数据列表
     * @param item
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询项目列表")
    @RequestMapping("/getItemList")
    @ResponseBody
    public Object getItemList(Item item, HttpServletRequest request) throws Exception{
        List<Item> list = service.getItemList(item);
        return new Result(list);
    }

    /**
     * 获取树列表
     * @param item
     * @param request
     * @return
     * @throws Exception
     */
    @Log("获取树列表")
    @RequestMapping("/getItemTreeList")
    @ResponseBody
    public Object getItemTreeList(Item item, HttpServletRequest request) throws Exception{
        List<Item> list = service.getItemTreeList(item);
        return new Result(list);
    }

    /**
     * 获取分页数据
     * @param item
     * @param request
     * @return
     * @throws Exception
     */
    @Log("分页查询项目列表")
    @RequestMapping("/getItemPage")
    @ResponseBody
    public Object getItemPage(Item item, HttpServletRequest request) throws Exception{
        Page page = CommonUtil.getPageFromRequest(request);
        List<Item> list = service.getItemPage(item, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询项目")
    @RequestMapping("/getItem")
    @ResponseBody
    public Object getItem(String id, HttpServletRequest request) throws Exception{
        Item item = service.getItem(id);
        return new Result(item);
    }

    /**
     * 新增
     * @param item
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增项目")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Item item, HttpServletRequest request) throws Exception{
        int num = service.insertItem(item);
        return new Result();
    }

    /**
     * 修改
     * @param item
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改项目")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Item item, HttpServletRequest request) throws Exception{
        int num = service.updateItem(item);
        return new Result();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("删除项目")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            int num = service.deleteItem(idArr);
        }else if(StringUtils.isNotBlank(id)){
            int num = service.deleteItem(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

}